import { createApp } from 'vue'
import App from './App.vue'
import ElementPlus from 'element-plus'
import 'element-plus/dist/index.css'
import router from './router'
import './api/request'
import * as ElementPlusIconsVue from '@element-plus/icons-vue'

// 增强 Element Plus 配置，降低 ResizeObserver 触发频率
const app = createApp(App)
app.use(ElementPlus, {
  // 添加全局配置，可能有助于减少 ResizeObserver 相关错误
  size: 'default',
  zIndex: 3000
})
app.use(router)

// 创建全局属性用于控制 ResizeObserver 行为
app.config.globalProperties.$_disableResizeObserver = false

// 在所有组件挂载前解决 ResizeObserver 循环错误
const fixResizeObserver = () => {
  // 替换原生 ResizeObserver 以防止循环错误
  const originalResizeObserver = window.ResizeObserver;
  
  if (originalResizeObserver) {
    window.ResizeObserver = class ResizeObserverWrapper extends originalResizeObserver {
      constructor(callback) {
        // 创建一个节流版本的回调函数
        const throttledCallback = throttle((...args) => {
          // 当禁用标志关闭时，才调用回调
          if (!app.config.globalProperties.$_disableResizeObserver) {
            try {
              callback(...args);
            } catch (e) {
              console.warn('ResizeObserver 回调出错，已捕获', e);
            }
          }
        }, 16); // 约 60fps 的频率
    
        super(throttledCallback);
      }
    }
  }
  
  // 节流函数
  function throttle(func, limit) {
    let lastFunc, lastRan;
    return function(...args) {
      if (!lastRan) {
        func(...args);
        lastRan = Date.now();
      } else {
        clearTimeout(lastFunc);
        lastFunc = setTimeout(function() {
          if ((Date.now() - lastRan) >= limit) {
            func(...args);
            lastRan = Date.now();
          }
        }, limit - (Date.now() - lastRan));
      }
    }
  }
}

// 执行修复
fixResizeObserver();

// 全局处理 ResizeObserver 循环错误
const originalWindowErrorHandler = window.onerror;
window.onerror = function(message, source, lineno, colno, error) {
  if (message.includes('ResizeObserver loop') || 
     (error && error.message && error.message.includes('ResizeObserver loop'))) {
    console.warn('已拦截 ResizeObserver 循环错误');
    
    // 临时禁用 ResizeObserver 回调以中断循环
    app.config.globalProperties.$_disableResizeObserver = true;
    
    // 一段时间后重新启用
    setTimeout(() => {
      app.config.globalProperties.$_disableResizeObserver = false;
    }, 100);
    
    return true; // 阻止错误继续传播
  }
  
  if (originalWindowErrorHandler) {
    return originalWindowErrorHandler.apply(this, arguments);
  }
  
  return false;
};

for (const [key, component] of Object.entries(ElementPlusIconsVue)) {
  app.component(key, component)
}

// 阻止浏览器控制台的 undelivered notifications 错误
const observer = new MutationObserver(() => {
  const resizeObserverErrDiv = document.getElementById('webpack-dev-server-client-overlay');
  if (resizeObserverErrDiv) {
    const text = resizeObserverErrDiv.innerText;
    if (text && text.includes('ResizeObserver loop')) {
      resizeObserverErrDiv.style.display = 'none';
      document.body.style.overflow = 'auto';
    }
  }
});
observer.observe(document.body, { childList: true, subtree: true });

app.mount('#app')
